package com.ljh.gateway;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author: Themberfue
 * @date: 2025/4/22 14:04
 * @description:
 */
@Slf4j
@Component
public class CustomGatewayFilterFactory extends AbstractGatewayFilterFactory<CustomConfig> implements Ordered {
    /**
     * 构造方法
     */
    public CustomGatewayFilterFactory() {
        super(CustomConfig.class);
    }

    @Override
    public GatewayFilter apply(CustomConfig config) {
        return new GatewayFilter() {
            /**
             * @param exchange ServerWebExchange：HTTP 请求-响应交互契约，提供了对 HTTP请求和响应的访问
             * @param chain GatewayFilterChain 过滤器链
             * @return Mono: Reactor的核心类，数据流发布者，Mono最多只能触发一个事件。可以把 Mono用在异步完成任务时，发出通知
             * chain.filter(exchange)  执行请求
             * Mono.fromRunnable()  创建一个包含 Runnable元素的数据流
             */
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
                // TODO 1. Pre 请求
                log.info("Pre Filter, config: {} ", config);  // ? Pre类型过滤器代码逻辑

                // TODO 2. 执行请求
                return chain.filter(exchange)

                        // TODO 3. Post 类型
                        .then(Mono.fromRunnable(() -> {
                            log.info("Post Filter....");  // ? Post类型过滤器代码逻辑
                        }));
            }
        };
    }

    @Override
    public int getOrder() {
        return Ordered.LOWEST_PRECEDENCE;
    }
}
